/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao.implement;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import model.dao.database.Database;
import model.dao.entiti.Pendonor;
import model.dao.iface.PendonorIface;

/**
 *
 * @author compaq
 */
public class PendonorImplement implements PendonorIface {

    public void tambahPendonor(Pendonor p) {
        try{
            PreparedStatement ps = Database.getConnection().prepareStatement("INSERT INTO pendonor VALUES(?,?,?,?,?)");
            ps.setString(1,p.getId());
            ps.setString(2,p.getNama());
            ps.setString(3,p.getUmur());
            ps.setString(4,p.getJenkel());
            ps.setString(5,p.getGoldarah());
            ps.executeUpdate();
        }
        catch(Exception e){
            
        }
    }

    public void hapusPendonor(String id) {
        try{
            PreparedStatement ps = Database.getConnection().prepareStatement("DELETE FROM Pendonor WHERE id=?");
            ps.setString(1, id);
            ps.executeUpdate();
        }
        catch(Exception e){
            
        }
    }

    public Pendonor bacaPendonor(String id) {
         try{
            PreparedStatement ps = Database.getConnection().prepareStatement("SELECT * FROM pendonor WHERE id=?");
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            Pendonor p = new Pendonor();
            if (rs.next()) {
                p.setId(rs.getString("id"));
                p.setNama(rs.getString("nama"));
                p.setUmur(rs.getString("umur"));
                p.setJenkel(rs.getString("jenkel"));
                p.setGoldarah(rs.getString("goldarah"));
            } 
            return p ;
        }
        catch(Exception e){
            return null ;
        }
    }

    public void ubahPendonor(String id, Pendonor p) {
        try{
           PreparedStatement ps = Database.getConnection().prepareStatement("UPDATE pendonor SET nama=?,umur=?,jenkel=?,goldarah=? WHERE id=?");
           ps.setString(1,p.getNama());
           ps.setString(2,p.getUmur());
           ps.setString(3,p.getJenkel());
           ps.setString(4,p.getGoldarah());
           ps.setString(5,id);
           ps.executeUpdate();
        }
        catch(Exception e){
            
        }
    }

    public List<Pendonor> bacaSemuaPendonor() {
        try{
            Statement st = Database.getConnection().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM pendonor") ;
            List<Pendonor> list = new ArrayList<Pendonor>() ;
            while (rs.next()) {
                Pendonor p = new Pendonor();
                p.setId(rs.getString("id"));
                p.setId(rs.getString("nama"));
                p.setId(rs.getString("umur"));
                p.setId(rs.getString("jenkel"));
                p.setId(rs.getString("goldarah"));
                list.add(p);
            } 
            return list ;
        }
        catch(Exception e){
           return null; 
        }
    }
    
}
